package org.ww.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.ww.common.utils.PageUtils;
import org.ww.common.utils.Query;
import org.ww.coupon.dao.SeckillSessionDao;
import org.ww.coupon.entity.SeckillSessionEntity;
import org.ww.coupon.entity.SeckillSkuRelationEntity;
import org.ww.coupon.service.SeckillSessionService;
import org.ww.coupon.service.SeckillSkuRelationService;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLates3DaySession() {
        LambdaQueryWrapper<SeckillSessionEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.between(SeckillSessionEntity::getStartTime,getStartTime(),getEndTime());
        List<SeckillSessionEntity> seckillSessionEntities = baseMapper.selectList(wrapper);
        if(CollectionUtils.isNotEmpty(seckillSessionEntities)){
            List<Long> ids = Optional.ofNullable(seckillSessionEntities)
                    .orElseGet(ArrayList::new)
                    .stream()
                    .map(SeckillSessionEntity::getId)
                    .distinct()
                    .collect(Collectors.toList());

            List<SeckillSkuRelationEntity> skuRelationEntities = seckillSkuRelationService.selectPromotionSessionIds(ids);

            for (SeckillSessionEntity seckillSessionEntity : seckillSessionEntities) {
                List<SeckillSkuRelationEntity> collect = Optional.ofNullable(skuRelationEntities)
                        .orElseGet(ArrayList::new)
                        .stream()
                        .filter(s -> s.getPromotionSessionId().equals(seckillSessionEntity.getId()))
                        .collect(Collectors.toList());
                seckillSessionEntity.setRelationSkus(collect);
            }
        }
        return seckillSessionEntities;
    }

    private String getStartTime(){
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        LocalDateTime startTime = LocalDateTime.of(now, min);
        return startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    private String getEndTime(){
        LocalDate now = LocalDate.now();
        LocalTime max = LocalTime.MAX;
        LocalDate endLocalDate = now.plusDays(2);
        LocalDateTime endTime = LocalDateTime.of(endLocalDate, max);
        return endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

}